import java.util.Arrays;
import java.util.Stack;

public class Solution735 {
    public int[] asteroidCollision(int[] asteroids) {
        Stack<Integer> stack=new Stack<>();
        for (int asteroid : asteroids) {
            if (asteroid>0){
                stack.push(asteroid);
            }else {
                while (!stack.isEmpty() && stack.peek()>0  && asteroid!=0){
                    if (stack.peek()+asteroid>0){
                        asteroid=0;
                    }else if (stack.peek()+asteroid<0){
                        stack.pop();
                    }else {
                        stack.pop();
                        asteroid=0;
                    }
                }
                if (asteroid!=0){
                    stack.push(asteroid);
                }
            }
        }
        int[] ans=new int[stack.size()];
        for (int i=0;i<stack.size();i++){
            ans[i]=stack.get(i);
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution735().asteroidCollision(new int[]{5, 10, -5})));
    }
}
